package com.ychong.library.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import com.ychong.library.R
import com.ychong.library.databinding.DialogPermissionTipsBinding
import com.ychong.library.listener.OnListener
import com.ychong.library.utils.ScreenUtils

class PermissionTipsDialog : Dialog, View.OnClickListener {
    private var binding: DialogPermissionTipsBinding? = null
    private var isCan: Boolean = false
    private var dimAmount: Float = 0.2f
    private var onListener: OnListener? = null
    private var title:String? = "是否允许“芯球管理”获取以下权限"

     fun setOnListener(onListener: OnListener): PermissionTipsDialog {
        this.onListener = onListener
        return this
    }


    constructor(context: Context) : super(context) {}

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding =
            DataBindingUtil.inflate(layoutInflater, R.layout.dialog_permission_tips, null, false)
        setContentView(binding!!.root)
        initData()
        initListener()
    }

    private fun initData() {
        setCancelable(false)
        setCanceledOnTouchOutside(false)
        val lp = window?.attributes
        lp!!.width = ScreenUtils.screenWidth - 100
        window?.attributes = lp
        window?.setWindowAnimations(R.style.alpha_center_animation)
        window?.setBackgroundDrawableResource(android.R.color.transparent)
    }

    private fun initListener() {
        binding!!.knowTv.setOnClickListener(this)
    }

    fun setTitle(title:String): PermissionTipsDialog {
        this.title = title
        return this
    }
    fun build(): PermissionTipsDialog {
        show()
        setViewData()
        return this
    }

    private fun setViewData() {
        setCancelable(isCan)
        setCanceledOnTouchOutside(isCan)
        window?.setDimAmount(this.dimAmount)

        binding!!.titleTv.text = this.title

    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.know_tv ->{
                if (onListener!=null){
                    dismiss()
                    onListener!!.listener()
                }
            }
        }
    }
}